﻿function Analyse(container, config) {
    this.forTime = [];

    this.forecastValueInForecastModel1 = [];
    this.forecastValueInForecastModel2 = [];
    this.forecastValueInForecastModel3 = [];

    this.pollutantName = [];

    this.series = [];
    this.seriesParam = config["seriesParam"] || [];
    this.height = config["height"] || null;
    this.width = config["width"] || null;
    this.xwidth = config["xwidth"] || 60;

    this.title = config["title"] || "";
    this.useDefaultTip = config["useDefaultTip"] || false;
    this.doubleAxis = config["doubleAxis"] || false;
    this.coAxis = config["coAxis"] || false;
    this.yAxis = [];
    this.audit = config["audit"] || false;



    this.container = container;
}

Analyse.prototype.tooltipFormatter = function (tooltip) {
    var ret = '<middle>' + tooltip.x + '</middle><br>';
    ret += '<table>';

    Highcharts.each(tooltip.points, function (point) {
        var series = point.series;
        ret += '<tr><td><span style="color:' + series.color + '">\u25CF</span> ' + series.name + '</td><td>' + Highcharts.pick(point.point.value, point.y) + '</td></tr>';
    });

    ret += '</table>';

    return ret;

    var series = tooltip.point.series, point = tooltip.point;
    return series.name + ":" + Highcharts.pick(point.value, point.y);
};


Analyse.prototype.getChartOptions = function () {
    var _ = this;
    return {
        chart: {
            renderTo: _.container,
            height: _.height,
            width: _.width
        },
        title: {
            text:  _.container + '对比分析图' || '对比分析图'
        },
        credits: {
            enabled: false
        },
        xAxis: [{
            categories: _.forTime
        }],
        yAxis: {
            title: {
                text: null
            },
            labels: {
                style: {
                    fontSize: '10px'
                }
            }
        },
        tooltip: {
            shared: true,
            crosshairs: true,
            useHTML: true,
            formatter: function () {
                return _.tooltipFormatter(this);
            }
        },
        series: _.series
    }
};

Analyse.prototype.onChartLoad = function () {
};

Analyse.prototype.createChart = function () {
    var _ = this;
    this.chart = new Highcharts.Chart(_.getChartOptions(), function (chart) {
        _.onChartLoad();
    });
};

Analyse.prototype.initData = function (jsonData) {
    var _ = this;
    if (jsonData.length > 0) {
        var d = jsonData;
        for (var i = 0; i < jsonData.length; i++) {

            _.forecastValueInForecastModel1.push(d[i].ForecastValueInForecastModel1);
            _.forecastValueInForecastModel2.push(d[i].ForecastValueInForecastModel2);
            _.forecastValueInForecastModel3.push(d[i].ForecastValueInForecastModel3);
          
   
            _.pollutantName.push(d[i].PollutantName);
          
            _.forTime.push(_.formatDate(_.GetTimeFromJsonString(d[i].ForTime), "MM月DD日"));
        }
        _.series = _.getSeries(_.series);
        _.createChart();
    }
    else {
        return "no data";
    }
};

Analyse.prototype.getSeries = function (series, people) {
    var _ = this, mul = "多元回归模型", sam = "同期回归模型", bp = "BP神经网络";
    if (_.people) {
        mul += _.peopleLang;
        sam += _.peopleLang;
        bp += _.peopleLang;
    }
    else {
        mul += "预报值";
        sam += "预报值";
        bp += "预报值";
    }
    series = [];

    series.push({
        id: 'model1Line',
        name: mul,
        color: '#7CB5EC',
        type: 'line',
        data: _.forecastValueInForecastModel1
    });

    series.push({
        id: 'model2Line',
        name: sam,
        color: '#90ED7D',
        type: 'line',
        data: _.forecastValueInForecastModel2
    });
    series.push({
        id: 'model3Line',
        name: bp,
        color: '#F45B5B',
        type: 'line',
        data: _.forecastValueInForecastModel3
    });

    return series;
}

Analyse.prototype.formatDate = function (date, format) {
    if (arguments.length < 2 && !date.getTime) {
        format = date;
        date = new Date();
    }
    typeof format != 'string' && (format = 'YYYY年MM月DD日 hh时mm分ss秒');
    var week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', '日', '一', '二', '三', '四', '五', '六'];
    return format.replace(/YYYY|YY|MM|DD|hh|mm|ss|星期|周|www|week/g, function (a) {
        switch (a) {
            case "YYYY": return date.getFullYear();
            case "YY": return (date.getFullYear() + "").slice(2);
            case "MM": return date.getMonth() + 1;
            case "DD": return date.getDate();
            case "hh": return date.getHours();
            case "mm": return date.getMinutes();
            case "ss": return date.getSeconds();
            case "星期": return "星期" + week[date.getDay() + 7];
            case "周": return "周" + week[date.getDay() + 7];
            case "week": return week[date.getDay()];
            case "www": return week[date.getDay()].slice(0, 3);
        }
    });
}

//将json时间转换成javascript时间

Analyse.prototype.GetTimeFromJsonString = function (jsonTime) {
    var re = /-?\d+/;
    var m = re.exec(jsonTime);
    var d = new Date(parseInt(m[0]));
    return d;
}